package com.gz.mylife.widget;


import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;

import com.gz.mylife.Define;
import com.gz.mylife.R;
import com.gz.mylife.app.SplashScreen;
import com.zl.smartmall.library.account.IAuth;

public class OfflineDialog extends RelativeLayout implements OnClickListener {

	private Dialog mOfflineDialog;
	private Context mContext;
	
	public OfflineDialog(Context context) {
		super(context);
		mContext = context;
	}

	public OfflineDialog(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
	}

	public OfflineDialog(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
	}
	
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		findViewById(R.id.btn_ok).setOnClickListener(this);
		mOfflineDialog = new Dialog(mContext, R.style.DialogTheme);
		mOfflineDialog.setContentView(this);
		mOfflineDialog.setCanceledOnTouchOutside(false);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_ok:
			reLogin();
			mOfflineDialog.dismiss();
//			setVisibility(GONE);
//			break;
			
		} 
	}
	
	public boolean isShowing() {
		return mOfflineDialog.isShowing();
	}

	public void reLogin(){
//		Global.get().logout();
//		FileUtils.deleteUidFromDir(mContext);
		IAuth.getInstance().clearUserInfo();
		mContext.sendBroadcast(new Intent(Define.ACTION_EXIT_WHOLE_APP));
		mContext.startActivity(new Intent(mContext, SplashScreen.class));
	}
	
	public void dismiss() {
		mOfflineDialog.dismiss();
	}
	
	public void show(){
//		setVisibility(VISIBLE);
		mOfflineDialog.show();
	}

	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
//			setVisibility(View.GONE);
			mOfflineDialog.dismiss();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

}
